package com.vf.admin.miaodong.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppCommentReply;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.entity.AppUserDressup;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * 购买坐骑
 */
@Repository
public class AfterUserDressupDao extends BaseDao<AppUserDressup> {

    /**
     * 综合查询列表
     */
    public List<AppUserDressup> list(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user_dressup where 1=1  ");
        if(StringEx.isNotNull(paraMap.get("startDate"))){
            sbStr.append(" and DATE_FORMAT(buyingtime, '%Y-%m-%d')  >=  ? ");
            paramList.add(paraMap.get("startDate"));
        }
        if(StringEx.isNotNull(paraMap.get("endDate"))){
            sbStr.append(" and DATE_FORMAT(buyingtime, '%Y-%m-%d')  <=  ? ");
            paramList.add(paraMap.get("endDate"));
        }
        if (StringEx.isNotNull(paraMap.get("userid"))) {
            sbStr.append(" and userid = ? ");
            paramList.add(paraMap.get("userid"));
        }
        if(StringEx.isNotNull(paraMap.get("nick"))){
            sbStr.append(" and nick like ? ");
            paramList.add("%"+paraMap.get("nick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("phone"))){
            sbStr.append(" and phone = ? ");
            paramList.add(paraMap.get("phone"));
        }
        if(StringEx.isNotNull(paraMap.get("type"))){
            sbStr.append(" and type = ? ");
            paramList.add(paraMap.get("type"));
        }
        if(StringEx.isNotNull(paraMap.get("state"))){
            sbStr.append(" and state = ? ");
            paramList.add(paraMap.get("state"));
        }
        sbStr.append(" order by buyingtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppUserDressup.class,splitPage);
    }


    /**
     * 详情
     */
    public AppUserDressup get(Integer id) {
        if (id==null || id<=0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user_dressup where id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppUserDressup.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppUserDressup haoYou) {
        if (haoYou!=null && haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id!=null && id <= 0) {
            return;
        }
        String sql = "delete  from app_user_dressup where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
     * 获取用户未过期的坐骑、头饰信息
     * @param map
     * @param bean
     * @return
     */
    public List<AppUserDressup> findWeiGuoQiList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM app_user_dressup a where 1=1 ");
        if (StringEx.isNotNull(map.get("userid"))) {
            sbStr.append(" and a.userid = ? ");
            paramList.add(map.get("userid"));
        }
        if (StringEx.isNotNull(map.get("type"))) {
            sbStr.append(" and a.type = ? ");
            paramList.add(map.get("type"));
        }
        sbStr.append(" and a.overtime > Now() order by a.buyingtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUserDressup.class, bean);
    }

    /**
     * 删除所有已过期的坐骑、头饰记录
     * @return
     */
    public void delAllGuoQi() {
        String sql = " DELETE  FROM app_user_dressup WHERE overtime<NOW() ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        this.update(sql,paramList);
    }

}
